package com.example.shiro2.user.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.shiro2.common.parameter.ResultDTO;
import com.example.shiro2.user.entity.SysUser;
import com.example.shiro2.user.entity.vo.SysUserVo;
import com.example.shiro2.user.service.SysUserService;
import com.example.shiro2.utils.Md5PassWordUtil;
import com.example.shiro2.utils.NullIs;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-06-26
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录")
    public ResultDTO loginUser(@RequestBody SysUserVo sysUserVo,HttpSession session) {
        System.out.println((String) session.getAttribute("code"));
        if(!NullIs.isEmpty(sysUserVo.getUsername())){
            SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", sysUserVo.getUsername()));
            if(NullIs.isEmpty(sysUser)){
                return ResultDTO.USER_NOT_EXIST();
            }
            if (!session.getAttribute("code").equals(sysUserVo.getCode())){
                return ResultDTO.error().message("验证码错误");
            }
            if(sysUserVo.getPassword().equals(sysUser.getPassword())){
                return ResultDTO.success().data(sysUser).message("登录成功");
            }else{
                return ResultDTO.USER_LOGIN_ERROR();
            }
        }
        return ResultDTO.success().message("权限验证成功");
    }

    @ApiOperation(value = "增加")
    @PostMapping("/insert")
    public ResultDTO<IPage<SysUser>> insert(@RequestBody SysUser sysUser) {
        SysUser old = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", sysUser.getUsername()));
        if(!NullIs.isEmpty(old)){
            return ResultDTO.bad().message("用户名已被注册");
        }
        SysUser old2 = sysUserService.getOne(new QueryWrapper<SysUser>().eq("phone", sysUser.getPhone()));
        if(!NullIs.isEmpty(old2)){
            return ResultDTO.bad().message("手机号已被注册");
        }
        sysUser.setPassword(Md5PassWordUtil.getMd5(sysUser.getPassword()));
        boolean boo = sysUserService.save(sysUser);
        return ResultDTO.tf(boo);
    }
    @GetMapping("getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        //验证码放入session
        System.out.println(captcha.getCode());
        session.setAttribute("code", captcha.getCode());
        //写出到浏览器（Servlet输出）
        System.out.println(session.getAttribute("code"));
        captcha.write(response.getOutputStream());
    }

}
